package com.winchain.chainbill.bill.biz.transfer.rx;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.BillAcctService;
import com.winchain.chainbill.trade.service.TradeService;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillXfeRevRejectAssetSubscriber extends OnErrorNotImplementedSubscriber<BillTranEvent<CbXfe>> {
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private TradeService tradeService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;

	public void onApplicationEvent(BillTranEvent<CbXfe> t) {
		Bill<CbXfe> billXfe = t.getBill();

		List<Bill<CbXfe>> child = (List<Bill<CbXfe>>) t.getChildren();
		CbXfe cbXfe = new CbXfe();
		BigDecimal xfeAmt = new BigDecimal(0);
		if ((child == null) || (child.size() == 0)) {
			cbXfe = (CbXfe) billXfe.getSource();
			xfeAmt = billXfe.getRec().getBillAmt();
		} else {
			for (Bill<CbXfe> bill : child) {
				if (((CbXfe) bill.getSource()).getXfeId() != null) {
					cbXfe = (CbXfe) bill.getSource();
					xfeAmt = xfeAmt.add(cbXfe.getBillAmt());
				}
			}
		}
		String bizType = "cb_xfe_rev";
		String optType = "qt_ubk";
		AcctTxRec tx = getTxRec(cbXfe, xfeAmt, bizType, optType);

		tx.setAcctin(false);
		this.tradeService.callback(tx);
	}

	private AcctTxRec getTxRec(CbXfe cbXfe, BigDecimal xfeAmt, String bizType, String optType) {
		AcctTxRec txRec = new AcctTxRec();
		txRec.setTxId(SystemUtil.getUUID());
		txRec.setTxSn(txRec.getTxId());
		txRec.setTxAmt(xfeAmt);
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs("������������������������");
		txRec.setTxType(bizType);
		txRec.setOptType(optType);
		txRec.setTxSts("S");
		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setBizId(cbXfe.getBillId());
		txRec.setDataScLvl("B");
		txRec.setCurrCode(AssetCode.XCB.name());
		txRec.setTxChnCode("C");
		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");
		txRec.setTxOperator(cbXfe.getBuyerId());

		AcctBas acct = this.billAcctService.getAcctBas(cbXfe.getBuyerId(), "1", null);
		txRec.setTxAcctNo(acct.getAcctNo());
		txRec.setTxAcctNm(acct.getAcctNm());
		txRec.setTxBankNm(acct.getFiNm());
		AcctBkp txAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getTxAcctNo(), txRec.getCurrCode(), null, null);
		txRec.setTxBkpId(txAcctBkp.getBkpId());

		return txRec;
	}
}
